package com.sunjy.demo1.controller;

import com.netflix.hystrix.HystrixCircuitBreaker;
import com.netflix.hystrix.HystrixCommandKey;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.sunjy.common.annotation.BeanValid;
import com.sunjy.common.domain.ApiResponseT;
import com.sunjy.common.domain.HeaderEnum;
import com.sunjy.demo1.feign.TestPersonClose2Feign;
import com.sunjy.demo1.feign.TestPersonCloseFeign;
import com.sunjy.demo1.feign.TestPersonFeign;
import com.sunjy.demo1.form.PersonForm;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Auther: sunjy
 * @Date: 2018/10/22 19:22
 * @Description:
 */
@RestController
@RequestMapping("hystrix")
@Slf4j
public class TestHystrixController {

    @Autowired
    private TestPersonFeign testPersonFeign;
    
    @Autowired
    private TestPersonCloseFeign testPersonCloseFeign;
    @Autowired
    private TestPersonClose2Feign testPersonClose2Feign;

    /**
     * 测试服务端限流
     * @return
     */
    @BeanValid
    @HystrixCommand(fallbackMethod = "error", commandProperties = {
            @HystrixProperty(name="execution.isolation.strategy", value = "THREAD"),
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "4000"),
            @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "10"),
            @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "40")
    }, threadPoolProperties = {
            @HystrixProperty(name = "coreSize", value = "1"),
            @HystrixProperty(name = "maxQueueSize", value = "10"),
            @HystrixProperty(name = "keepAliveTimeMinutes", value = "1000"),
            @HystrixProperty(name = "queueSizeRejectionThreshold", value = "8"),
            @HystrixProperty(name = "metrics.rollingStats.numBuckets", value = "12"),
            @HystrixProperty(name = "metrics.rollingStats.timeInMilliseconds", value = "1440")
    })
    @GetMapping(value = "/hello", produces = {"application/json;charset=UTF-8"})
    public ApiResponseT<Object> hello(){
        try {
            Thread.sleep(1000000);
        } catch (InterruptedException e) {
            return ApiResponseT.error();
        }
        return ApiResponseT.success();

    }

    public ApiResponseT<Object> error() {
        return ApiResponseT.error(HeaderEnum.HYSTRIX_ERROR);
    }

    /**
     * 测试客户端熔断器 get请求
     * @return
     */
    @GetMapping(value = "/testGet", produces = {"application/json;charset=UTF-8"})
    public ApiResponseT<Object> testGet(){
    	ApiResponseT result= testPersonFeign.findOnePerson(10,null);
    	HystrixCircuitBreaker breaker = HystrixCircuitBreaker.Factory
                .getInstance(HystrixCommandKey.Factory
                        .asKey("TestPersonFeign#findOnePerson()"));
    	return result;
        

    }

    /**
     * 测试客户端熔断器 post请求
     * @return
     */
    @GetMapping(value = "/testPost", produces = {"application/json;charset=UTF-8"})
    public ApiResponseT<Object> testPost(){
    	ApiResponseT repsonse=null;
        try {
            PersonForm form=new PersonForm();
            form.setId(20);
            form.setName("sunjy");
            repsonse= testPersonFeign.findPostPerson(form);
        } catch (Exception e) {
            return ApiResponseT.error();
        }
        return repsonse;

    }
    
    /**
     * 测试feign关闭hystrix
     * @return
     */
    @GetMapping(value = "/testFeignClose", produces = {"application/json;charset=UTF-8"})
    public ApiResponseT<Object> testFeignClose(){
    	ApiResponseT repsonse=null;
        try {
            PersonForm form=new PersonForm();
            form.setId(20);
            form.setName("sunjy");
            repsonse= testPersonCloseFeign.findPostPerson(form);
        } catch (Exception e) {
            return ApiResponseT.error();
        }
        return repsonse;

    }
    
    /**
     * 测试feign关闭hystrix
     * @return
     */
    @GetMapping(value = "/testFeignClose2", produces = {"application/json;charset=UTF-8"})
    public ApiResponseT<Object> testFeignClose2(){
    	ApiResponseT repsonse=null;
        try {
            PersonForm form=new PersonForm();
            form.setId(20);
            form.setName("sunjy");
            repsonse= testPersonClose2Feign.findPostPerson(form);
        } catch (Exception e) {
            return ApiResponseT.error();
        }
        return repsonse;

    }
    
    



}
